package com.sdkj.recruit.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.IMongodbDAO;
import com.sdkj.recruit.dao.IUserDao;
import com.sdkj.recruit.model.Paginate;
import com.sdkj.recruit.model.SmsCode;
import com.sdkj.recruit.model.User;
import com.sdkj.recruit.mongoSupport.QueryParamCreator;
import com.sdkj.recruit.util.StringUtil;


@Persistent
@Service("userDao")
public class UserDao implements IUserDao {

	@Autowired
	private IMongodbDAO dao;

	@Override
	public User querUser() {

		dao.getPO(User.class, QueryParamCreator.eq("userId", "1"));

		return null;
	}

	@Override
	public User queryUserByName(String userName) {
		return dao.getPO(User.class, QueryParamCreator.eq("userName", userName));
	}

	@Override
	public void saveUsers(User user) {
		dao.saveOrUpdatePO(user);
	}

	@Override
	public void deleteUserById(String userId) {
		dao.deletePOList(User.class, QueryParamCreator.eq("userId", userId));
	}

	@Override
	public void cleanUserLoginInfo(String userId) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("accessToken", null);
		map.put("isOnline", false);
		map.put("expireTime", 0);
		dao.saveOrUpdateDocument(User.class, map, QueryParamCreator.eq("userId", userId));
	}

	@Override
	public void updateUserLoginInfo(String accessToken, Date lastLoginTime,long expireTime,String userId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("accessToken", accessToken);
		map.put("tokenTime", lastLoginTime);
		map.put("lastLoginTime", lastLoginTime);
		map.put("expireTime", expireTime);
		dao.saveOrUpdateDocument(User.class, map, QueryParamCreator.eq("userId", userId));
	}

	@Override
	public User queryUserById(String userId) {
		return dao.getPO(User.class, QueryParamCreator.eq("userId",userId));
	}

	@Override
	public void updatePassword(String userId, String createNewpassword) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("password", createNewpassword);
		dao.saveOrUpdateDocument(User.class, map, QueryParamCreator.eq("userId", userId));
	}

	@Override
	public User queryUserByToken(String token) {
		return dao.getPO(User.class, QueryParamCreator.eq("accessToken",token));
	}

	@Override
	public void updateUserProperty(String userId, Map<String, Object> property) {
		dao.saveOrUpdateDocument(User.class, property, QueryParamCreator.eq("userId",userId));
	}

	@Override
	public List<User> queryUserList(int pageNo, int pageSize, String keyWord) {
		List<Order> orders = new ArrayList<Order>();
        Order order = new Order(Direction.DESC, "userType");
        Order order1 = new Order(Direction.DESC, "createTime");
        orders.add(order);
        orders.add(order1);
        List<Criteria> params = new ArrayList<Criteria>();
        params.add(QueryParamCreator.ne("userId", null));
        
        if(StringUtil.isNotEmptyString(keyWord)){
            params.add(QueryParamCreator.like("userName", keyWord));
            
        }
        return dao.getPOList(User.class, new Paginate(pageNo, pageSize), orders, params.toArray(new Criteria[0]));
	}

	@Override
	public long queryAllUserCount(String keyWord) {
		List<Criteria> params = new ArrayList<Criteria>();
		if (StringUtil.isNotEmptyString(keyWord)) {
			params.add(QueryParamCreator.like("userName", keyWord));
		}
		params.add(QueryParamCreator.ne("userId", null));
		return dao.getRowCount(User.class, params.toArray(new Criteria[0]));
	}

	@Override
	public User queryUserByEmCode(String emCode) {
		
		return dao.getPO(User.class, QueryParamCreator.eq("emCode", emCode));
	}

	@Override
	public User queryUserByPhone(String mobile) {
		return dao.getPO(User.class, QueryParamCreator.eq("mobile", mobile));
	}

	@Override
	public void saveSmsInfo(SmsCode sms) {
		dao.saveOrUpdatePO(sms);
	}

	@Override
	public SmsCode querySms(String mobile,int codeType) {
		List<Order> orders = new ArrayList<Order>();
        Order order = new Order(Direction.DESC, "sendTime");
        orders.add(order);
        List<Criteria> params = new ArrayList<Criteria>();
        params.add(QueryParamCreator.eq("phone", mobile));
        params.add(QueryParamCreator.eq("codeType", codeType));
        List<SmsCode> list =dao.getPOList(SmsCode.class, new Paginate(1, 1), orders, params.toArray(new Criteria[0]));
        if(!CollectionUtils.isEmpty(list)) {
        	return list.get(0);
        }
        return null;
	}

}
